﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class cms_AddNewPromotionImage : System.Web.UI.Page
{
    private DBClass db = new DBClass();
    private int PromoId = 0;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Params["PromoId"] != null)
        {
            Int32.TryParse(Request.Params["PromoId"].ToString(), out PromoId);
        }
        if (!IsPostBack)
        {
            LoadData();
        }
    }

    private void LoadData()
    {
        ddlType.DataSource = db.GetAllType();
        ddlType.DataBind();
        LoadInfoPromotion();
    }

    private void LoadInfoPromotion()
    {
        if (PromoId > 0)
        {
            DataTable dt = db.GetInfoPromotionById(PromoId);
            if (dt != null && dt.Rows.Count > 0)
            {
                DataRow row = dt.Rows[0];
                txtDescription.Text = row["PromotionDescription"].ToString();
                ddlType.SelectedValue = row["TypeId"].ToString();
                ImagePromo.ImageUrl = "~/images/HomeSlide/" + row["Image"].ToString();
            }
        }
    }

    private void UploadImage()
    {
        if (FileUpload1.HasFile)
        {
            if (db.IsFormatImage(FileUpload1.FileName))
            {
                FileUpload1.SaveAs(Server.MapPath("~/images/HomeSlide/" + FileUpload1.FileName));
            }
        }
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        UploadImage();
        int TypeId=0;
        Int32.TryParse(ddlType.SelectedValue,out TypeId);
        if (PromoId > 0)
        {
            db.UpdatePromotionImage(txtDescription.Text, FileUpload1.FileName, TypeId, PromoId);
        }
        else
        {
            db.InsertPromotionImage(txtDescription.Text, FileUpload1.FileName, TypeId);
        }

        Response.Redirect("Promotion.aspx");
    }
}